# Running a wavelet computation on a methane molecule¶

The purpose of this lesson is to get familiar with the basic variables needed to run a wavelet computation in isolated boundary conditions. At the end of the lesson, you can run a wavelet run, check the amount of needed memory and understand the important part of the output. We propose to use python in a Jupyter notebook to simplify the use of the bigdft executable and to have tools to combine pre-processing, execution, post-processing and analysis.

## Introduction: Running the code¶

As already explained in the N2 molecule tutorial.bigdft uses dictionaries (a collection of pairs of key and value) for the input and output which are serialized by means of the yaml format. This allows us to manipulate dictionaries instead of files and define simple actions on them for the basic code operations, instead of manually modifying the input keys.

We here introduce the BigDFT.Inputfiles.Inpufile class, which is a generalisation of a python dictionary and that has as class methods the same methods that are available in the BigDFT.InputActions module.

[20]:

#the dictionary below:
#inp = { 'dft': { 'hgrids': 0.55, 'rmult': [3.5, 9.0]} }
# becomes
from BigDFT import Inputfiles as I
inp=I.Inputfile()
inp.set_hgrid(0.55)
inp.set_rmult([3.5,9.0])


Beside a default input file called input.yaml, BigDFT requires the atomic positions for the studied system and optionaly the pseudo-potential files. For the following tutorial, a methane molecule will be used. The position file is a simple XYZ file named CH4_posinp.xyz:

5  angstroemd0  # methane molecule
free
C        0           0           0
H       -0.63169789 -0.63169789 -0.63169789
H       +0.63169789 +0.63169789 -0.63169789
H       +0.63169789 -0.63169789 +0.63169789
H       -0.63169789 +0.63169789 +0.63169789


We can copy this file into the default posinp file posinp.xyz as cp CH4_posinp.xyzz posinp.xyz or indicate it in the posinp keyword of the calculator options

Running BigDFT is done using the bigdft executable in a standard Unix way. In this notebook, we use the SystemCalculator class:

[44]:

from BigDFT import Calculators as calc
from BigDFT.Database import Molecules as mols
study = calc.SystemCalculator(skip=True)#Create a calculator
study.update_global_options(posinp=mols.Molecule('CH4'))
ch4 = study.run(input=inp)

Initialize a Calculator with OMP_NUM_THREADS=1 and command /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft
Creating the yaml input file "./input.yaml"
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -s Yes


ch4 is the instance of the class Logfile which can handle easily all information coming from the output file log.yaml. Then we can display some information as:

[45]:

print (ch4)

- Atom types:
- C
- H
- cell: Free BC
- forcemax_cv: 0.0
- symmetry: disabled
- energy: -8.026155368542689
- grid_spacing: 0.55
- XC_parameter: 1
- nat: 5
- forcemax: 0.01861869071424
- dipole:
- -0.004520998
- -0.004520998
- -0.004520998
- spin_polarization: 1
- system_charge: 0
- number_of_orbitals: 4
- rmult:
- 3.5
- 9.0
- fermi_level: -0.3425340751278
- posinp_file: input.yaml
- total_magn_moment: 0
- gnrm_cv: 0.0001
- No. of KS orbitals:
- 4



# The wavelet basis set, a convergence study¶

Daubechies Wavelets is a systematic basis set (as plane waves are), which means than one can increase arbitrarily the accuracy of the results by varying some parameters which are defined in the dft dictionary (inp['dft']). We here explain what are the meaning and the typical values for the principal parameters, hgrid and rmult.

hgrids are used to set up the basis set. In free boundary conditions, the basis set is characterised by a spatial expansion and a grid step, as shown in the side figure. There is ‘’one float value’’ describing the ‘’grid steps’’ in the three space directions (‘’i.e.’’ x, y and z) or a 3D array is also accepted. These values are in bohr unit and typically range from 0.3 to 0.65. The harder the pseudo-potential, the lower value should be set up. These values are called hgrids in the input dictionary, and can be set by the set_hgrid method of the Inpufile class.

rmult contains an array of two float values that are two multiplying factors. They multiply quantities that are chemical species dependent. The first factor is the most important since it describes ‘’the spatial expansion’’ of the basis set (in yellow on the figure beside). Indeed the basis set is defined as a set of real space points with non-zero values. These points are on a global regular mesh and located inside spheres centered on atoms. The first multiplying factor is called crmult for Coarse grid Radius MULTiplier. Increasing it means that further spatial expansion is possible for the wavefunctions. Typical values are 5 to 7. The second one called frmult for Fine grid Radius MULTiplier is related to the fine resolution. This parameter is less pertinent for the convergence of energy and can be ignored. It is possible to indicate only one float value, the crmult parameter. Such parameters can be set by the method set_rmult of Inputfile class.

## Exercise¶

Run BigDFT for the following values of hgrid and crmult and plot the total energy convergence versus hgrids. The final total energy can be retrieved using th emethod energy from the result of each of the runs. The unit of the energies is in Hartree. This tutorial also explains how to use the Dataset class.

[46]:

hgrids = [0.55, 0.5, 0.45, 0.4, 0.35, 0.3, 0.25, 0.2] #bohr
crmult = [3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0]


This precision plot shows the systematicity of the wavelet basis set: by improving the basis set, we improve the absolute value of the total energy.

Indeed, there are two kind of errors arising from the basis set. The first one is due to the fact the basis set can’t account for quickly varying wavefunctions (value of hgrids should be decreased). The second error is the fact that the wavefunctions are constrained to stay inside the defined basis set (output values are zero). In the last case crmult should be raised.

# Construction of the input dataset dictionaries¶

Let us build three different dataset of inputfiles, the first two varying hgrid and crmult and the last by varying the two together. We also label each of the input dictionaries by a unique name identifying the run.

[47]:

from BigDFT import Datasets as D
import copy
h_and_c_dataset=D.Dataset('h_and_c')
for h,c in zip(hgrids,crmult):
inp_run=copy.deepcopy(inp)
inp_run.set_hgrid(h)
inp_run.set_rmult([c,9.0])
h_and_c_dataset.append_run(id={'h':h, 'c': c},input=inp_run,runner=study)

[48]:

h_only_dataset=D.Dataset('h_only')
for h in hgrids:
inp_run=copy.deepcopy(inp)
inp_run.set_hgrid(h)
h_only_dataset.append_run(id={'h':h},input=inp_run,runner=study)

[49]:

c_only_dataset=D.Dataset('c_only')
for c in crmult:
inp_run=copy.deepcopy(inp)
inp_run.set_rmult([c,9.0])
c_only_dataset.append_run(id={'c':c},input=inp_run,runner=study)


We then run the three datasets:

[50]:

h_only_dataset.run()
c_only_dataset.run()
h_and_c_dataset.run()

Creating the yaml input file "runs/h:0.55.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n h:0.55 -s Yes
Creating the yaml input file "runs/h:0.5.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n h:0.5 -s Yes
Creating the yaml input file "runs/h:0.45.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n h:0.45 -s Yes
Creating the yaml input file "runs/h:0.4.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n h:0.4 -s Yes
Creating the yaml input file "runs/h:0.35.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n h:0.35 -s Yes
Creating the yaml input file "runs/h:0.3.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n h:0.3 -s Yes
Creating the yaml input file "runs/h:0.25.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n h:0.25 -s Yes
Creating the yaml input file "runs/h:0.2.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n h:0.2 -s Yes
Creating the yaml input file "runs/c:3.5.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n c:3.5 -s Yes
Creating the yaml input file "runs/c:4.0.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n c:4.0 -s Yes
Creating the yaml input file "runs/c:4.5.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n c:4.5 -s Yes
Creating the yaml input file "runs/c:5.0.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n c:5.0 -s Yes
Creating the yaml input file "runs/c:5.5.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n c:5.5 -s Yes
Creating the yaml input file "runs/c:6.0.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n c:6.0 -s Yes
Creating the yaml input file "runs/c:6.5.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n c:6.5 -s Yes
Creating the yaml input file "runs/c:7.0.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n c:7.0 -s Yes
Creating the yaml input file "runs/c:3.5,h:0.55.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n c:3.5,h:0.55 -s Yes
Creating the yaml input file "runs/c:4.0,h:0.5.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n c:4.0,h:0.5 -s Yes
Creating the yaml input file "runs/c:4.5,h:0.45.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n c:4.5,h:0.45 -s Yes
Creating the yaml input file "runs/c:5.0,h:0.4.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n c:5.0,h:0.4 -s Yes
Creating the yaml input file "runs/c:5.5,h:0.35.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n c:5.5,h:0.35 -s Yes
Creating the yaml input file "runs/c:6.0,h:0.3.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n c:6.0,h:0.3 -s Yes
Creating the yaml input file "runs/c:6.5,h:0.25.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n c:6.5,h:0.25 -s Yes
Creating the yaml input file "runs/c:7.0,h:0.2.yaml"
Run directory runs
Executing command:  /home/genovese/binaries/1.9.0-dawson/install/bin/bigdft -n c:7.0,h:0.2 -s Yes

[50]:

{0: <BigDFT.Logfiles.Logfile instance at 0x7f1f4fdf7c30>,
1: <BigDFT.Logfiles.Logfile instance at 0x7f1f4fe0cfa0>,
2: <BigDFT.Logfiles.Logfile instance at 0x7f1f4fd92050>,
3: <BigDFT.Logfiles.Logfile instance at 0x7f1f4fdf7820>,
4: <BigDFT.Logfiles.Logfile instance at 0x7f1f4fdc3550>,
5: <BigDFT.Logfiles.Logfile instance at 0x7f1f4fd5cbe0>,
6: <BigDFT.Logfiles.Logfile instance at 0x7f1f4fd72dc0>,
7: <BigDFT.Logfiles.Logfile instance at 0x7f1f4fd14500>}


We now store the energies of each of the dataset runs, and identify the minimum as the minimum value from the h_and_c dataset:

[51]:

from numpy import array as arr
energies_h=arr(h_only_dataset.fetch_results(attribute='energy'))
energies_c=arr(c_only_dataset.fetch_results(attribute='energy'))
energies_hc=arr(h_and_c_dataset.fetch_results(attribute='energy'))
#find the minimum
emin=min(energies_hc)


We plot the energy values varying the grid spacing or the extension

[52]:

import matplotlib.pyplot as plt
%matplotlib inline
plt.xlabel('Grid step (Bohr)')
plt.plot(hgrids,energies_h-emin,label='crmult=3.5')
plt.plot(hgrids,energies_hc-emin,label='varying hgrids+crmult')
plt.yscale('log')
plt.legend(loc='best')

[52]:

<matplotlib.legend.Legend at 0x7f1f4fcf0650>

[53]:

plt.xlabel('Crmult value')
plt.plot(crmult,energies_c-emin,label='hgrid=0.55')
plt.plot(crmult,energies_hc-emin,label='varying hgrids+crmult')
plt.yscale('log')
plt.legend(loc='best')

[53]:

<matplotlib.legend.Legend at 0x7f1f4fbbfad0>


## Considerations¶

We see that both the parameters hgrids and rmult have to be decreased and increased (respectively) in order to achieve convergence. Increasing only one of the two parameter will eventually lead to saturation of the absolute error on the energy.

## Fine tuning of the basis set¶

The multi-scale property of the wavelets is used in BigDFT and a two level grid is used for the calculation. We’ve seen previously the coarse grid definition using the the multiplying factor rmult. The second multiplying value on this line of the input file is used for the fine grid and is called frmult. Like crmult, it defines a factor for the radii used to define the fine grid region where the number of degrees of freedom is indeed eight times the one of the coarse grid. It allows to define region near the atoms where the wavefunctions are allowed to vary more quickly. Typical values for this factor are 8 to 10. It’s worth to note that even if the value of the multiplier is greater than crmult it defines a smaller region due to the fact that the units which are associated to these radii are significantly different.

The physical quantities used by crmult and frmult can be changed in the pseudo-potential by adding an additional line with two values in bohr. The two values that the code is using (either computed or read from the pseudo-potential files) are output in the following way in the screen output:

 - Symbol                              : C #Type No.  01
No. of Electrons                    :  4
No. of Atoms                        :  1
Coarse                            :  1.58437
Fine                              :  0.30452
Coarse PSP                        :  1.30510
Source                            : Hard-Coded
- Symbol                              : H #Type No.  02
No. of Electrons                    :  1
No. of Atoms                        :  4
Coarse                            :  1.46342
Fine                              :  0.20000
Coarse PSP                        :  0.00000
Source                            : Hard-Coded

## Analysing the output¶

The output of BigDFT is divided into four parts: * Input values are printed out, including a summary of the different input files (DFT calculation parameters, atom positions, pseudo-potential values…); * Input wavefunction creation, usually called “input guess”; * The SCF (Self-Consistent Field) loop itself; * The post SCF calculations including the forces calculation and other possible treatment like a finite size effect estimation or a virtual states determination.

### The system parameters output¶

All the read values from the different input files are printed out at the program startup. Some additional values are provided there also, like the memory consumption. Values are given for one process, which corresponds to one core in an MPI environment.

[ ]:

import yaml
print yaml.dump(ch4.memory,default_flow_style=False)

print 'Estimated Memory Peak',ch4.memory_peak,'MB'


The overall memory requirement needed for this calculation is thus: 39 MB (Estimated Memory Peak) which is provided by thememory_peak attribute.

In this example, the memory requirement is given for one process run and the peak of memory will be in the initialisation during the Kernel calculation, while the SCF loop will reach 36MB during the Poisson solver calculation. For bigger systems, with more orbitals, the peak of memory is usually reached during the Hamiltonian application.

### Exercise¶

Run a script to estimate the memory requirement of a run before submitting it to the queue system of a super-computer using the dry_run option.

It reads the same input, and is thus convenient to validate inputs.

Try several values from 1 to 6 and discuss the memory distribution.

[ ]:

study = calc.SystemCalculator(dry_run=True) #Create a calculator
peak = []
for i in range(1,7):
study.set_global_option('dry_mpi',i)
dry = study.run(input=inp)
peak.append(dry.memory_peak)
print yaml.dump(dry.memory,default_flow_style=False)
for i,p in enumerate(peak):
print "mpi=",i+1,"Estimated memory peak (MB)",p


BigDFT distributes the orbitals over the available processes. The value All (distributed) orbitals does not decrease anymore after 4 processes since there are only 4 bands in our example). This means that running a parallel job with more processors than orbitals will result in a bad speedup. The number of cores involved in the calculation might be however increased via OMP parallelisation, as it is indicated in Scalability with MPI and OpenMP lesson.

## The input guess¶

The initial wavefunctions in BigDFT are calculated using the atomic orbitals for all the electrons of the s, p, d shells, obtained from the solution of the PSP self-consistent equation for the isolated atom.

[ ]:

print yaml.dump(ch4.log['Input Hamiltonian'])


The corresponding hamiltonian is then diagonalised and the n_band (norb in the code notations) lower eigenfunctions are used to start the SCF loop. BigDFT outputs the eigenvalues, in the following example, 8 electrons were used in the input guess and the resulting first fourth eigenfunctions will be used for a four band calculation.

Input Guess Overlap Matrices: {Calculated: true, Diagonalized: true}
Orbitals:
- {e: -0.6493539153869, f: 2.0}
- {e: -0.3625626366055, f: 2.0}
- {e: -0.3624675839372, f: 2.0}
- {e: -0.3624675839372, f: 2.0} -- Last InputGuess eval, H-L IG gap:  20.6959 eV
- {e: 0.3980916655348, f: 0.0}  -- First virtual eval
- {e: 0.3983087771728, f: 0.0}
- {e: 0.3983087771728, f: 0.0}
- {e: 0.5993393223683, f: 0.0}


## The SCF loop¶

The SCF loop follows a direct minimisation scheme and is made of the following steps: * Calculate the charge density from the previous wavefunctions. * Apply the Poisson solver to obtain the Hartree potential from the charges and calculate the exchange-correlation energy and the energy of the XC potential thanks to the chosen functional. * Apply the resulting hamiltonian on the current wavefunctions. * Precondition the result and apply a steepest descent or a DIIS history method. This depends on idsx, not specified in the present input. It is therefore set to the default value, which is 6 (for an history of 6 previous set of vectors. To perform a SD minimisation one should add “idsx: 0” to the dft dictionary of inp. * Orthogonalise the new wavefunctions.

Finally the total energy and the square norm of the residue (gnrm) are printed out. The gnrm value is the stopping criterion. It can be chosen using gnrm_cv in the dft dictionary. The default value (1e-4) is used here and a good value can reach 1e-5.

[ ]:

print 'gnrm_cv by default',ch4.gnrm_cv


The minimisation scheme coupled with DIIS (and thanks to the good preconditioner) is a very efficient way to obtain convergence for systems with a gap, even with a very small one. Usual run should reach the 1e-4 stop criterion within 15 to 25 iterations. Otherwise, there is an issue with the system, either there is no gap, or the input guess is too symmetric due to the LCAO diagonalization, specific spin polarization…

[ ]:

ch4.wfn_plot()


### The post-SCF treatments¶

At the end of the SCF loop, a diagonalisation of the current hamiltonian is done to obtain Kohn-Sham eigenfunctions. The corresponding eigenvalues are also given.

[ ]:

print ch4.evals


The forces are then calculated.

[ ]:

print yaml.dump(ch4.forces,default_flow_style=False)


Some other post-SCF may be done depending on the parameters in the dft dictionary of inp.

### Exercise¶

Run bigdft when varying the DIIS history length and discuss the memory consumption.

Reducing the DIIS history is a good way to reduce the memory consumption when one cannot increase the number of processes. Of course this implies more iterations in SCF loops.

BigDFT can treat charged system without the requirement to add a compensating background like in plane waves. The additional charge to add to the system is set in the dft dictionary with the qcharge key. In the following example an electron has been added (-1):

[ ]:

inp3 = {}
inp3['dft'] = { 'hgrids': 0.55, 'nrepmax': 'accurate' }
inp3['posinp'] = 'CH3-_posinp.xyz'


### Exercise¶

Remove the last hydrogen atom in the previous methane example and modify to add an electron. Then run BigDFT for an electronic convergence.

One can notice that the total charge in the system is indeed -8 thanks to the additional charge. The convergence rate is still good for this CH$$_3^-$$ radical since it is a closed shell system.

[ ]:

study = calc.SystemCalculator() #Use a new calculator)
ch3m = study.run(input=inp3)
print ch3m
ch3m.wfn_plot()


## Running a geometry optimisation¶

In the previous charged example the geometry of the radical is kept the same than for the methane molecule, while it is likely to change. One can thus optimize the geometry with BigDFT.

To run geometry calculations (molecular dynamics, structure optimisations…) one should add another dictionary geopt in the input which contains the method to use.

In the log file, all input variables are indicated with their default value.

Here, we look for a local minimum so we can use the keyword LBFGS. We can add also the stopping criteria. There are two stopping criteria: the first ncount_cluster_x being the number of loops (force evaluations) and the second forcemax is the maximum on forces. For isolated systems, the first criterion is well adapted while the second is good for periodic boundary conditions.

[ ]:

inpg = {}
inpg['dft'] = { 'hgrids': 0.55, 'nrepmax': 'accurate' }
inpg['posinp'] = 'CH4_posinp.xyz'
inpg['geopt'] = { 'method': 'LBFGS', 'ncount_cluster_x': 20}
study = calc.SystemCalculator() #Use a new calculator)
ch4geopt = study.run(input=inpg)

[ ]:

%matplotlib inline
ch4geopt.geopt_plot()


### Exercise¶

Take the CH$$_3^-$$ radical **CH3-_posinp.xyz** file and run a geometry optimisation.

The evolution of the forces during relaxation can be easily obtained using the geop_plot function to the result of the calculation.

At each iteration, BigDFT outputs a file posout_XXXX.xyz in the directory data with the geometry of the iteration XXX. You can visualize it using v_sim (select all files in the “Browser” tab).